#include <QtGui>
#include <QSettings>


#include "mainwindow.h"
#include "ui_mainwindow.h"



MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    calendar = NULL;
    windowView = NULL;
    windowViewLayout = NULL;
    calViewGroup = NULL;
    calViewLayout = NULL;
    testLabel = NULL;

    createOption();
    readSetting();
    ui->setupUi(this);
    setObjectName(tr("Wcanlender"));

    createCalView();
    createWindowView();


    createActions();
    createMenu();

}

MainWindow::~MainWindow()
{
    writeSetting();
    delete ui;
}

void MainWindow::createMenu()
{
    viewMenu =  menuBar()->addMenu(tr("View"));
    viewMenu->addAction(viewYearAct);
    viewMenu->addSeparator();

    optionMenu = menuBar()->addMenu(tr("Option"));

}

void MainWindow::createCalView()
{
    //calViewLayout = new QGridLayout;
    isCalViewEnable = true;


    if (!calendar)
    {
        calendar = new QCalendarWidget;
    }
    if (!calViewGroup)
    {
        calViewGroup = new QGroupBox;
    }
    if (!calViewLayout)
    {
        calViewLayout = new QGridLayout;
    }


    //calViewLayout->addWidget(calendar, 0, 0);
    if (calOption->viewMode == CalOption::CalViewModeByMonth)
    {
        //setCentralWidget(calendar);
        qDebug("<<1 count is %d", calViewLayout->count());
        if (calViewLayout->count() > 0)
        {
            calViewLayout->removeWidget(calViewLayout->itemAt(0)->widget());
            delete testLabel;
            testLabel = NULL;
        }

        calViewLayout->addWidget(calendar);
        qDebug(">>1 count is %d", calViewLayout->count());
    } else {

        //setCentralWidget(NULL);
        qDebug(">>2count is %d", calViewLayout->count());
        if (calViewLayout->count() > 0)
        {
            calViewLayout->removeWidget(calViewLayout->itemAt(0)->widget());
            delete calendar;
            calendar = NULL;
        }
        //add year view mode here.
        testLabel = new QLabel(tr("test lable"));
        calViewLayout->addWidget(testLabel);
        qDebug("<<2count is %d", calViewLayout->count());
    }
    //calViewLayout->update();
    calViewGroup->setLayout(calViewLayout);
    //calViewGroup->update();

}


void MainWindow::createOption()
{
    calOption = new CalOption;
    //calOption->viewMode = CalOption::CalViewModeByYear;
}

void MainWindow::createActions()
{
    viewYearAct = new QAction("&Swith View", this);
    connect(viewYearAct, SIGNAL(triggered()), this, SLOT(switchView()));

    viewSeekAct = new QAction("S&eek for specific date", this);
    connect(viewSeekAct, SIGNAL(triggered()), this, SLOT(findView()));
}

void MainWindow::switch2YearView()
{
    //add switch2 year view function here.
    calOption->viewMode = CalOption::CalViewModeByYear;
}

void MainWindow::switch2MonthView()
{
    //add switch2 year view function here.
    calOption->viewMode = CalOption::CalViewModeByMonth;
}

void MainWindow::switchView()
{
    if(calOption->viewMode == CalOption::CalViewModeByMonth)
    {
        switch2YearView();
    }
    else
    {
        switch2MonthView();
    }
    createCalView();
    //createWindowView();
}

void MainWindow::findView()
{
    //add seek view function here


    QGroupBox *seekGroupBox = new QGroupBox(tr("Seek Date"));

    //QDateEdit *currentDateEdit = new QDateEdit;
    //QLabel *currentDateLabel = new QLabel(tr("Current Date:"));

    QGridLayout *layout = new QGridLayout;
    layout->addWidget(calendar);
    layout->addWidget(seekGroupBox);
    //setLayout(layout);
    //setCentralWidget(layout);


}

void MainWindow::readSetting()
{
    //add read setting fuction
    QSettings settings("fly2dream Co.", "Wcalendar");

    if (!calOption)
    {
        calOption = new CalOption;

    }
    //calOption = settings.value()
    calOption->viewMode = (CalOption::CalViewMode)settings.value("viewmode").toInt();

}

void MainWindow::writeSetting()
{
    //add write setting function
    QSettings settings("fly2dream Co.", "Wcalendar");

    settings.setValue("viewmode", calOption->viewMode);
}

void MainWindow::createWindowView()
{
    if (!windowView)
    {
        windowView = new QWidget;
    }
    windowViewLayout = new QGridLayout;
    //add calView
    if (isCalViewEnable == true)
    {
        windowViewLayout->addWidget(calViewGroup);
        //enable calview
    }
    if (isOptViewEnable == true)
    {
        //enable opt view
    }
    if (isFindViewEnable == true)
    {
        //enable find viwe
    }
    windowView->setLayout(windowViewLayout);
    setCentralWidget(windowView);

}
